import { Component, OnInit } from '@angular/core';
import { LocalStorage } from '@core/local.storage';
import { environment } from '../../../../environments/environment';
import { HttpClient } from '@angular/common/http';
import { DatePipe } from '@angular/common';
import { ExportFileService } from '@core/exportFile';

@Component({
  selector: 'app-dbwtzz',
  templateUrl: './dbwtzz.component.html',
  styleUrls: ['./dbwtzz.component.less']
})
// 督办问题追踪
export class DbwtzzComponent implements OnInit {
  tableData: any = [];
  pageTotal = 0;
  pageIndexTable = '1';
  selectedValue = '20';

  queryParam: any = {};
  lockHeadScroll = { y: window.innerHeight - 265 + 'px' };
  dropdownStyle = {
    height: '200px',
  };

  statusArr = [
    { opValue: '', opName: '全部' },
    { opValue: '0', opName: '未完成' },
    { opValue: '1', opName: '已完成' },
  ];

  sourceArr = [
    { opValue: '', opName: '全部' },
    { opValue: '0', opName: '总公司、集团公司' },
    { opValue: '1', opName: '段' },
  ];

  userInfo = JSON.parse(this.ls.get('loginInfo'));
  tableLoading: boolean;

  constructor(
    public ls: LocalStorage,
    private http: HttpClient,
    private exportFile: ExportFileService,
    private datePipe: DatePipe
  ) { }

  ngOnInit() {
    this.readyload(this.pageIndexTable, this.selectedValue);
  }

  selectedChange() {
    this.readyload(this.pageIndexTable, this.selectedValue);
  }

  rowChangeTable(ev) {
    this.pageIndexTable = ev + '';
    this.readyload(this.pageIndexTable, this.selectedValue);
  }

  doParam(param) {
    Object.keys(param).forEach(key => {
      if (!param[key] || key.indexOf('_') == 0) {
        delete param[key];
      }
    });
  }

  readyload(page, limit) {
    this.tableLoading = true;
    this.queryParam.page = page;
    this.queryParam.limit = limit;
    const dataurl = environment.apiBase + '/csmis/duty/getImpTrack.jwt';
    const param = JSON.parse(JSON.stringify(this.queryParam));
    if (this.queryParam._sDate) {
      param.BEGINDATE = this.datePipe.transform(this.queryParam._sDate, 'yyyy-MM-dd');
    }
    if (this.queryParam._eDate) {
      param.ENDDATE = this.datePipe.transform(this.queryParam._eDate, 'yyyy-MM-dd');
    }

    this.doParam(param);
    this.http.post(dataurl, param).subscribe((res: any) => {
      this.tableLoading = false;
      if (res && res.rows) {
        res.rows.forEach(row => {
          if (row.PROBLEM_TIME) {
            row._PROBLEM_TIME = this.datePipe.transform(row.PROBLEM_TIME, 'yyyy-MM-dd');
          }
          if (row.FINISHDATE) {
            row._FINISHDATE = this.datePipe.transform(row.FINISHDATE, 'yyyy-MM-dd');
          }
          if (row.VC_FINISH) {
            row._vcFinished = row.VC_FINISH == '1' ? '已完成' : '未完成';
          }
        });
        this.tableData = res.rows;
        this.pageTotal = res.total;
      }
    });
  }

  exportSheet() {
    let param = '';
    if (this.queryParam.FINISH) {
      param += '&FINISH=' + this.queryParam.FINISH;
    }
    if (this.queryParam.TYPE) {
      param += '&TYPE=' + this.queryParam.TYPE;
    }
    if (this.queryParam._sDate) {
      param += '&BEGINDATE=' + this.datePipe.transform(this.queryParam._sDate, 'yyyy-MM-dd');
    }
    if (this.queryParam._eDate) {
      param += '&ENDDATE=' + this.datePipe.transform(this.queryParam._eDate, 'yyyy-MM-dd');
    }
    if (param) {
      param = param.replace('&', '?');
    }
    this.exportFile.postDown(environment.apiBase + '/csmis/duty/exportImpTrack.jwt', param, '重点信息追踪' + this.datePipe.transform(new Date(), 'yyyy-MM-dd') + '.xls');
  }
}
